//
//  AppDelegate.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/9.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit
import UserNotifications//通知管理
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setupAddtions()

        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }
    

    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        
        print("\(url)")
        return true
    }

}

//设置应用信息额外信息
extension AppDelegate {

    fileprivate func setupAddtions(){
    
        //设置 SVProgressHUD 最小显示时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        if #available(iOS 10.0, *) {
            
            UNUserNotificationCenter.current().requestAuthorization(options: [.carPlay,.sound,.alert,.badge], completionHandler: { (isSuccess, error) in
                
                print("授权 \(isSuccess ? "成功":"失败")")
            })
        }
        else{
            //申请用户的通知授权，本地的。［上方提示 ／ 声音 ／ badge］
            let notificationSettings = UIUserNotificationSettings(types:[.badge,.alert,.sound], categories:nil)
            UIApplication.shared.registerUserNotificationSettings(notificationSettings)
        }
    }
}

extension AppDelegate {
    
    fileprivate func loadAppInfo(){
    
        //模拟异步加载
        DispatchQueue.global().async {
            
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
        
            let data = NSData(contentsOf: url!)
            
            let dire = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            let jsonpath = (dire as NSString).appendingPathComponent("main.json")
            
            data?.write(toFile:jsonpath, atomically: true)
            
//            print("json文件目录\(jsonpath)")
        }
        
        //FIXME: 测试的token，不用登录
        WBUserAccount().saveAccount()
    
    }
    
    
}
